//
//  OwnerViewController.m
//  GoodWayClient
//
//  Created by newhuidong on 16/1/4.
//  Copyright © 2016年 HuiDong. All rights reserved.
//
#import <UIKit/UIKit.h>

#import "OwnerViewController.h"
#import "YJYYCollectionView.h"
#import "AppDelegate.h"
#import "CarOwnerLeftMenuViewController.h"
#import "BlueSkyfundTableViewController.h"
#import "OwnerCleanKingViewController.h"
#import "OwnerCleanServiceViewController.h"
#import "NewsViewController.h"
#import "MyShopHomeViewController.h"
#import "AFNetworking.h"
#import "Constant.h"
#import "SDWebImageManager.h"
#import "SDWebImageDownloaderOperation.h"
#import "UIImageView+WebCache.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ImageHeight [UIScreen mainScreen].bounds.size.height/3.5
#define ImageCount  3


@interface OwnerViewController ()
@property(nonatomic,strong)NSMutableArray * mutableArray;
@property(nonatomic,strong)NSMutableArray * images;

//广告轮播
@property(nonatomic,strong)UILabel *textLabel;

@property(nonatomic,retain)NSTimer * advTimer;
@property(nonatomic,assign)int count;
@property(nonatomic,strong)NSArray * array;

@property(nonatomic,strong)UIView * backgroundView;
@property(nonatomic,strong)UIScrollView *scroller;
@property (nonatomic,strong)NSTimer * timer;
@property(nonatomic,strong) UIImageView * imageView;
@property(nonatomic,strong)UIPageControl * pageControl;
@end

@implementation OwnerViewController


-(NSArray *)array{
    if (!_array) {
        _array=@[@"路之好蓝天优惠活动",@"珠江路新世界中心",@"鲁迅原名周茂凯",@"马上上线",@"结束了"];
    }
    return _array;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initButton];
    [self createNews];
    //滚动字幕
    
    self.textLabel=[[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2.6, 13, 200, 30)];
    self.textLabel.font=[UIFont systemFontOfSize:15];
    [self.backgroundView addSubview:self.textLabel];
    
    UITapGestureRecognizer * tapGesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moveToNews:)];
    
    tapGesture.delegate=self;
    self.textLabel.userInteractionEnabled=YES;
    [self.textLabel addGestureRecognizer:tapGesture];
    [self loadScrollViewImage];
    
    NSUInteger totalCount =4;
    
    self.pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(50, [UIScreen mainScreen].bounds.size.height/3, ScreenWidth-100, 30)];
    self.pageControl.currentPageIndicatorTintColor=[UIColor colorWithRed:0/255.0f green:128/255.0 blue:255/255.0f alpha:1];
    //self.pageControl.backgroundColor=[UIColor blackColor];
    self.pageControl.numberOfPages=totalCount;
    [self.scroller addSubview:self.pageControl];
    
    
}

-(void)moveToNews:(UITapGestureRecognizer*)sender{
    
    NewsViewController * newsVC =[[NewsViewController alloc]init];
    [self presentViewController:newsVC animated:YES completion:nil];
    NSLog(@"点到我了");
    
    
    
}
/*广告轮播*/

- (void)timeChange
{
    if (self.count == (self.array.count-1)) {
        self.count = 0;
    }else{
        self.count++;
    }
    self.textLabel.text = self.array[self.count];
    self.textLabel.textColor=[UIColor darkGrayColor];
}

-(void)createNews{
    
    if (self.advTimer) {
        [self.advTimer invalidate];
        self.advTimer = nil;
    }
    self.advTimer = [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
-(void)initButton{
    //轮播广告
    self.backgroundView =[[UIView alloc]initWithFrame:CGRectMake(0, ImageHeight+64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/12)];
    self.backgroundView.backgroundColor=[UIColor whiteColor];
    self.backgroundView.layer.shadowColor=[UIColor blackColor].CGColor;
    self.backgroundView.layer.shadowOffset=CGSizeMake(2, 2);
    self.backgroundView.layer.shadowOpacity=0.8f;
    [self.view addSubview:self.backgroundView];
    
    
    UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
    imageView.image=[UIImage imageNamed:@"guangbo"];
    [self.backgroundView addSubview:imageView];
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(50, 10, 80, 30)];
    label.text=@"最新公告:";
    label.font=[UIFont systemFontOfSize:15];
    label.textColor=[UIColor colorWithRed:0/255.0f green:128/255.0f blue:255/255.0f alpha:1];
    [self.backgroundView addSubview:label];
    //蓝天基金按钮
    UIButton * button1=[UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame=CGRectMake(10, [UIScreen mainScreen].bounds.size.height/2+10, ScreenWidth/2.5+10, [UIScreen mainScreen].bounds.size.height/3);
    button1.backgroundColor=[UIColor whiteColor];
    [button1 setImage:[UIImage imageNamed:@"blueskyfund"] forState:UIControlStateNormal];
    [button1 setImageEdgeInsets:UIEdgeInsetsMake(20, 35, button1.frame.size.height/2, 35)];
    [button1 setTitle:@"蓝天基金" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [button1 setTitleEdgeInsets:UIEdgeInsetsMake(70, -110, 0, 0)];
    button1.layer.shadowColor=[UIColor blackColor].CGColor;
    button1.layer.shadowOffset=CGSizeMake(2, 2);
    button1.layer.shadowOpacity=.8f;
    [button1 addTarget:self action:@selector(goToBlueskyFund) forControlEvents:UIControlEventTouchUpInside];
    button1.layer.cornerRadius=5;
    [self.view addSubview:button1];
    
    
    UIButton * button2 =[UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame =CGRectMake(10+button1.frame.size.width+16, button1.frame.origin.y, button1.frame.size.width+10, [UIScreen mainScreen].bounds.size.height/6.5);
    button2.backgroundColor=[UIColor whiteColor];
    [button2 setImage:[UIImage imageNamed:@"cleanking"] forState:UIControlStateNormal];
    [button2 setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 85)];
    [button2 setTitle:@"除碳王" forState:UIControlStateNormal];
    button2.titleLabel.font=[UIFont systemFontOfSize:15];
    [button2 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [button2 setTitleEdgeInsets:UIEdgeInsetsMake(10, -40, 10, 10)];
    button2.layer.shadowColor=[UIColor blackColor].CGColor;
    button2.layer.shadowOffset=CGSizeMake(2, 2);
    button2.layer.shadowOpacity=.8f;
    [button2 addTarget:self action:@selector(goToCleanKing) forControlEvents:UIControlEventTouchUpInside];
    button2.layer.cornerRadius=5;
    [self.view addSubview:button2];
    
    UIButton * button3 =[UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame=CGRectMake(button2.frame.origin.x, button2.frame.origin.y+button2.frame.size.height+11, button2.frame.size.width, button2.frame.size.height);
    button3.backgroundColor=[UIColor whiteColor];
    [button3 setImage:[UIImage imageNamed:@"cleanservice"] forState:UIControlStateNormal];
    [button3 setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 85)];
    [button3 setTitle:@"除碳服务" forState:UIControlStateNormal];
    button3.titleLabel.font=[UIFont systemFontOfSize:15];
    [button3 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [button3 setTitleEdgeInsets:UIEdgeInsetsMake(10, -40, 10, 10)];
    button3.layer.shadowColor=[UIColor blackColor].CGColor;
    button3.layer.shadowOffset=CGSizeMake(2, 2);
    button3.layer.shadowOpacity=.8f;
    [button3 addTarget:self action:@selector(gotoCleanSer) forControlEvents:UIControlEventTouchUpInside];
    button3.layer.cornerRadius=5;
    [self.view addSubview:button3];
    
    UIButton * enterShop =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    enterShop.layer.cornerRadius=5;
    enterShop.frame=CGRectMake(10, [UIScreen mainScreen].bounds.size.height-60, [UIScreen mainScreen].bounds.size.width-20, 50);
    enterShop.backgroundColor=[UIColor colorWithRed:0 / 255.0 green:128 / 255.0 blue:255 / 255.0 alpha:1.0f];
    [enterShop setTitle:@"进入商城" forState:UIControlStateNormal];
    enterShop.titleLabel.font=[UIFont systemFontOfSize:20];
    [enterShop setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [enterShop addTarget:self action:@selector(goToShangCheng) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:enterShop];
    
}

-(void)goToShangCheng{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MyShopHomeViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyShopHomeViewController"];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)goToCleanKing{
    OwnerCleanKingViewController * cleanKing =[[OwnerCleanKingViewController alloc]init];
    [self.navigationController pushViewController:cleanKing animated:YES];
}

-(void)gotoCleanSer{
    UIStoryboard *story=[UIStoryboard  storyboardWithName:@"Main" bundle:nil];
   OwnerCleanServiceViewController *dtView = [story instantiateViewControllerWithIdentifier:@"OwnerCleanServiceViewController"];
    [self.navigationController pushViewController:dtView animated:YES];
}

-(void)goToBlueskyFund{
    
    UIStoryboard * story =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    BlueSkyfundTableViewController * bluesky =[story instantiateViewControllerWithIdentifier:@"BlueSkyfundTableViewController"];
    
    [self.navigationController pushViewController:bluesky animated:YES];
    
}
//打开侧边菜单
- (IBAction)LeftBarbtnPress:(id)sender {
    AppDelegate * tempAppDelegate = (AppDelegate *)
    [[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed) {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }else{
        [tempAppDelegate.LeftSlideVC closed];
    }
    NSLog(@"菜单");
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    /*改变系统导航栏颜色*/
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:0/255.0f green:128/255.0f blue:255/255.0f alpha:1.0f];
    /*自定义标题颜色*/
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    /*隐藏标题文字*/
    [[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    
}
-(void)loadScrollViewImage{
    
    AFHTTPRequestOperationManager * manmger =[AFHTTPRequestOperationManager manager];
    manmger.responseSerializer=[AFJSONResponseSerializer serializer];
    
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    AppDelegate*tempAppDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    dic[@"token"]=tempAppDelegate.token;
    dic[@"status"]=@0;
    [manmger POST:[NSString stringWithFormat:@"%@%@",url2,@"GetTopChangeImgs"] parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dic1=(NSDictionary *)responseObject;
        self.mutableArray =[[NSMutableArray alloc]init];
        self.mutableArray=[[dic1 valueForKey:@"Data"]valueForKey:@"list"];
        
        self.images=[self.mutableArray valueForKey:@"ImageUrl"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.scroller=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ImageHeight)];

            for (int i=0; i<self.images.count; i++) {
                NSString* iamge=[NSString stringWithFormat:@"%@%@",url3,[self.images objectAtIndex:i]];
                NSURL * imageUrl=[NSURL URLWithString:iamge];
                self.imageView=[[UIImageView alloc]init];
                self.imageView.frame=CGRectMake(i*ScreenWidth, 0, ScreenWidth, ImageHeight);
                 [self.imageView sd_setImageWithURL:imageUrl];
                 [self.scroller addSubview:self.imageView];

            }
            self.scroller.delegate=self;
            self.scroller.pagingEnabled=YES;
            self.scroller.contentSize=CGSizeMake(ScreenWidth*4, 0);
            self.scroller.backgroundColor=[UIColor whiteColor];
            self.scroller.showsHorizontalScrollIndicator=NO;
            [self.view addSubview:self.scroller];
            [self.scroller addSubview:self.imageView];
            [self.view addSubview:self.pageControl];
            [self starTimer];
        });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
    }];
}
-(void)starTimer{
    
    self.timer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(changePic) userInfo:nil repeats:YES];
}
-(void)changePic{
    
    int page =(int)self.pageControl.currentPage;
    
    if (page == 3) {
        page= 0;
    }else{
        page++;
    }
    
        CGFloat x = page *self.scroller.frame.size.width;
        self.scroller.contentOffset =CGPointMake(x, 0);

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
        CGFloat scrollviewW = self.scroller.frame.size.width;
        CGFloat x=self.scroller.contentOffset.x;
        int page =(x+scrollviewW/2)/scrollviewW;
        self.pageControl.currentPage=page;

    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self removeTimer];
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self starTimer];
}
-(void)removeTimer{
    
    [self.timer invalidate];
}
@end
